#include "Item.h"
#include "LoadLua.h"

Item::Item(void)
{
	ItemNo = no::NOTHING;
	ItemType = type::EMPTY;
	ItemDescription = "";
	ItemDescription2 = "";
}

Item::~Item(void)
{
}

Item::Item(no Number){
	ItemNo = Number;

	lua_State *L = lua_open(); 

	// load the libs 
	luaL_openlibs(L);

	if (luaL_loadfile(L, "misc.lua") || lua_pcall(L, 0, 0, 0)) 
	{ 
		printf("error: %s\n", lua_tostring(L, -1)); 
		//return -1; 
	}
	else
	{
		printf("Lua File Loaded\n");
	}

	lua_getglobal(L,"unpack_level");
	lua_pushstring(L,"items.csv");
	lua_pushnumber(L,Number+1);
	lua_call(L,2,5);

	this->ItemName = lua_tostring(L, -4);
	this->ItemType = (type)lua_tointeger(L, -3);
	this->ItemDescription = lua_tostring(L, -2);
	this->ItemDescription2 = lua_tostring(L, -1);

	lua_close(L);

	//ItemType = Type;
	//ItemDescription = Description;
}

void Item::SetItemNo(no Number)
{
	this->ItemNo = Number;
}

void Item::SetItemNull(void)
{
	ItemNo = Item::no::NOTHING;
	ItemType = Item::type::EMPTY;
	ItemName = "";
	ItemDescription = "";
}

Item::no Item::ReturnItemNo(void)
{
	return this->ItemNo;
}

void Item::SetItemType(type Type)
{
	this->ItemType = Type;
}

Item::type Item::ReturnItemType(void)
{
	return this->ItemType;
}

Item::type Item::ReturnItemType(no Number)
{
	lua_State *L = lua_open();

	luaL_openlibs(L);

	if (luaL_loadfile(L, "misc.lua") || lua_pcall(L, 0, 0, 0)) 
	{ 
		printf("error: %s\n", lua_tostring(L, -1)); 
		//return -1; 
	}
	else
	{
		printf("Lua File Loaded\n");
	}

	lua_getglobal(L,"unpack_level");
	lua_pushstring(L,"items.csv");
	lua_pushnumber(L,Number+1);
	lua_call(L,2,4);

	Item::type temp = (type)lua_tointeger(L, -2);

	lua_close(L);

	return temp;
}

void Item::SetItemDescription(string Description)
{
	this->ItemDescription = Description;
}

string Item::ReturnItemDescription(void)
{
	return this->ItemDescription;
}

void Item::SetItemDescription2(string Description)
{
	this->ItemDescription2 = Description;
}

string Item::ReturnItemDescription2(void)
{
	return this->ItemDescription2;
}

void Item::SetItemName(string Name)
{
	this->ItemName = Name;
}

string Item::ReturnItemName(void)
{
	return this->ItemName;
}